package cn.edu.cqupt.cxcy.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import cn.edu.cqupt.cxcy.dto.TeamImpl;
import cn.edu.cqupt.cxcy.model.Team;
import cn.edu.cqupt.cxcy.util.JsonUtil;
import cn.edu.cqupt.cxcy.util.Tools;

/**
 * Servlet implementation class TeamAddInfo
 */
@WebServlet("/TeamAddInfo")
public class TeamAddInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("text/html;charset=utf-8");
		HttpSession session = request.getSession();
	    List<Object> list = (List<Object>)session.getAttribute("user");
	    int te_id = (int) list.get(1);
		Team team = new Team();
		team.setTe_id(te_id);
		DiskFileItemFactory factory = new DiskFileItemFactory();
		String savePath = request.getServletContext().getRealPath("/teamlogo");
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("utf-8");
		File dir = new File(savePath);
		File file = null;
		if(!dir.exists()){
			dir.mkdirs();
		}
		factory.setRepository(dir);
		factory.setSizeThreshold(1024*1024*1024);
		try {
			List<FileItem> items = upload.parseRequest(request);
			for(FileItem item:items){
				if(!item.isFormField() && item != null && !item.getName().equals("") && item.getName() != null){
					String foot = item.getName().substring(item.getName().indexOf("."));
					String realFileName = Tools.getRandom(10)+foot;
					String teamlogo = "teamlogo/"+realFileName;
					file = new File(savePath,realFileName);
					item.write(file);
					team.setTe_logo(teamlogo);
				}else if(item.isFormField() && item != null){
					if(item.getFieldName().equals("teamName")){
						team.setTe_name(item.getString("utf-8"));
					}else if(item.getFieldName().equals("teamExplain")){
						team.setTe_explain(item.getString("utf-8"));
					}
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}catch(Exception e1){
			e1.printStackTrace();
		}
		TeamImpl teamImpl = new TeamImpl();
		teamImpl.addTeamInfo(team);	
		/*
		String pjax_back = "$(function(){$(\"a.turn_pjax[href='team/team']\").trigger('click');});";
		request.setAttribute("pjax_back", pjax_back);
		request.getRequestDispatcher("Tteam.jsp").forward(request, response);
		*/
		response.setContentType("application/json;charset=utf-8");
		response.setHeader("pragma","no-cache");
		response.setHeader("cache-control","no-cache");
		PrintWriter out = response.getWriter();
		JsonUtil json = new JsonUtil(); 
		json.put("photo", team.getTe_logo());
		json.put("tename", team.getTe_name());
		json.put("explain", team.getTe_explain());
		out.print(json);
		out.flush();
		out.close();
	}

}
